
const response = require("../utils/response")
const dao = require("../dao/comment")
const { checkauth } = require("../utils/check");

/**
 * 添加一条评论
 * @param {*} data {user_id, secret_id, content, level, post_id, father_id, reply_id}
 */
async function add_comment(data) {
    let auth = await checkauth(data.secret_id, data.user_id);
    if (!auth) {
        return response.forbidden("权限错误");
    }
    let cid = await dao.add_comment(data.user_id, data.content, data.level, data.post_id, data.father_id, data.reply_id);
    return response.ok({
        comment_id: cid
    })
}

/**
 * 删除一条评论
 * @param {*} data {comment_id}
 */
async function del_comment(data) {
    let user_id1 = await dao.get_user_id(data.comment_id);  // 评论主id
    let user_id2 = await dao.get_poster_id(data.comment_id);    // 帖主id
    let auth = (await checkauth(data.secret_id, user_id1)) || (await checkauth(data.secret_id, user_id2))
    if (!auth) {
        return response.forbidden("权限错误");
    } 
    await dao.del_comment(data.comment_id);
}

module.exports = {
    add_comment: add_comment,
    del_comment: del_comment,
}
